package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.SubmenuDAO;
import bist.believeinstep.domain.SubMenu;

import bist.believeinstep.mapper.SubmenuMapper;

public class SubmenuTemplate implements SubmenuDAO, ConnectionDAO{
	private JdbcTemplate jdbcTemplate;
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(SubMenu submenu){
		String query = "INSERT INTO bist_submenus "
				+ "(name, bist_menus_id, created_date, modified_date, created_by, modified_by)"
				+ "VALUES(?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
					submenu.getName(),
					submenu.getMenuId(),
					submenu.getCreated(),
					submenu.getModified(),
					submenu.getCreatedBy(),
					submenu.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(SubMenu submenu){
		String query = "UPDATE bist_submenus SET"
				+ "name = ?"
				+ "bist_menus_id = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				submenu.getName(),
				submenu.getMenuId(),
				submenu.getCreated(),
				submenu.getModified(),
				submenu.getCreatedBy(),
				submenu.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public SubMenu getSubMenu(Integer id){
		String query = "SELECT FROM bist_submenus WHERE id = ?";
		try{
			SubMenu submenu = jdbcTemplate.queryForObject(query, new Object[]{id}, new SubmenuMapper());	
			return submenu;
		}catch(Exception ex){
				ex.printStackTrace();			
		}
		return null;
	}
	@Override
	public SubMenu getSubMenu(String name){
		String query = "SELECT FROM bist_submenus WHERE name = ?";
		try{
			SubMenu submenu = jdbcTemplate.queryForObject(query, new Object[]{name}, new SubmenuMapper());	
			return submenu;
		}catch(Exception ex){
				ex.printStackTrace();			
		}
		return null;
	}
	@Override
	public List<SubMenu> list(){
		String query = "SELECT * FROM bist_submenus";
		try{
			List<SubMenu> submenu = jdbcTemplate.query(query, new SubmenuMapper());
			return submenu;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_submenus where id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
